﻿using UnityEngine;
using System.Collections;
using Pathfinding;

public class Citizen {

    private int _waitTime = 0;
    private Quaternion rot;
    private Quaternion toTarget;
    private Vector3 euler;
    private Path _Ppath;
    private int _currentWayPoint;

    /// <summary>
    /// Game Object of the NPC
    /// </summary>
	public GameObject oCitizen { get; set; }

    /// <summary>
    /// Controller of movement
    /// </summary>
	public CharacterController cController { get; set; }

	public int CurrentWayPoint
	{
		get{ return _currentWayPoint; }
		set { _currentWayPoint = value;}
	}
	
	public Path path 
	{
		get { return _Ppath;}
		set { _Ppath = value;}
	}

    public int WaitTime
    {
        get { return _waitTime; }
        set { _waitTime = value; }
    }

	public void Talk()
	{
        
	}
	
    /// <summary>
    /// Make NPC move to the next direction
    /// </summary>
    /// <param name="nextPoint">Next point to the NPC go</param>
	public void Walk(Vector3 nextPoint)
	{
		cController.SimpleMove (nextPoint);
	}
    
    /// <summary>
    /// Make the NPC rotate to the direction of the nwxt point
    /// </summary>
    /// <param name="nextPoint">Next point to the NPC go</param>
    /// <param name="turningSpeed">Speed to make the NPC turn</param>
	public virtual void RotateTowards (Vector3 nextPoint, float turningSpeed) {
        if (nextPoint != Vector3.zero)
        {
            rot = oCitizen.transform.rotation;
            toTarget = Quaternion.LookRotation(nextPoint);
            rot = Quaternion.Slerp(rot, toTarget, turningSpeed * Time.fixedDeltaTime);
            euler = rot.eulerAngles;
            euler.z = 0;
            euler.x = 0;
            rot = Quaternion.Euler(euler);
            oCitizen.transform.rotation = rot;
        }
	}

    /// <summary>
    /// Check the path is complete
    /// </summary>
    /// <param name="p"></param>
    public void OnPathComplete(Path p)
    {
        if (!p.error)
        {
            path = p;
            CurrentWayPoint = 0;
        }
    }
}
